<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col :span="20">
      {{ data.name }}
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handle">
            <span>操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!data.root">
                <el-dropdown-item command="sub">编辑部分</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentsListAPI } from '@/api/departments'
export default {
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    handle (e) {
      if (e === 'add') {
        this.$emit('toggel-box', {
          isShow: true,
          pid: this.data.id,
          type: 'add'
        })
      } else if (e === 'sub') {
        this.$emit('toggel-box', {
          isShow: true,
          pid: this.data.id,
          type: 'sub'
        })
      } else if (e === 'del') {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delDepartmentsListAPI(this.data.id)
          })
          .then((ret) => {
            if (ret.code === 99999) this.$message.error(ret.message)
            this.$message.success(ret.message)
            return this.$emit('upDateLoad')
          })
          .catch((err) => {
            if (err === 'cancel') return this.$message.error('已取消')
            return this.$message.error('网络出错!')
          })
      }
    }
  }
}
</script>
